package net.dgg.iboss.sc.backservice.task.quartz;

import net.dgg.iboss.base.util.DateUtil;
import net.dgg.iboss.sc.backservice.task.service.CertificateOutTimeOrPunishService;
import net.dgg.iboss.sc.backservice.task.service.CommonService;
import net.dgg.iboss.sc.backservice.task.service.GeneralCredentailService;
import net.dgg.tmd.foundation.platform.common.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 证书超期定时器
 *
 * @author wmj
 * @date 2018/9/5 0005
 */
@Component
public class GeneralCredentialOverDueJob {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private GeneralCredentailService generalCredentailService;
    @Autowired
    private CertificateOutTimeOrPunishService certificateOutTimeOrPunishService;

    @Autowired
    private CommonService commonService;

    /**
     * 证书超期提醒/预警（每周一10点）
     * @author zihao.wang
     */
    @Scheduled(cron = "0 0 10 ? * 1")
    public void execute() {
        try {
           generalCredentailService.dealOverDueGeneralCredentail();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("证书超期定时器判定失败：" + e.getMessage());
        }
    }

    /*
     *  每天定时更新资质状态
     */
    @Scheduled(cron = "0 30 5 * * ?")
//    @Scheduled(cron = "0 0/30 * * * ??")
    public void executeUpdateQualificationStatus() {
        try {
            generalCredentailService.updateQualificationOverdueStatus();
        } catch (Exception e) {
            logger.error("资质超期定时更新执行失败", e.getMessage(), e);
        }
    }

    /**
     * 证书出库超期提醒
     */
    @Scheduled(cron = "0 0 10 * * ?")
    //@Scheduled(cron = "0 0/5 * * * ?")
    public void zsMatchingOutAlert() {
        try {
            if(commonService.isWorkDay(new Date())) {
                certificateOutTimeOrPunishService.outTimeAlert();
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("证书出库超期提醒出现异常！",e.getMessage(),e);
        }
    }
    @Scheduled(cron = "0 0 18 * * ?")
    //@Scheduled(cron = "0 0/5 * * * ?")
    public void zsMatchingPunish() {
        try {
            if(commonService.isWorkDay(new Date())) {
                certificateOutTimeOrPunishService.outTimePunish();
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("证书出库超期扣罚出现异常！",e.getMessage(),e);
        }
    }
}
